package com.crm.model.dto;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.common.Dictionary;
import com.crm.model.entity.contractManager.CustomerContract;
import com.crm.model.entity.customerManager.Customer;
import com.crm.model.entity.customerManager.CustomerProductDetail;
import com.crm.model.entity.customerManager.CustomerProductDetailMemberCard;
import com.crm.model.entity.customerManager.CustomerProductDetailThroughTrainTax;
import com.crm.model.entity.finishOrderProduct.FinishOrderProduct;
import com.crm.model.entity.productManager.CustomerMemberProduct;
import com.crm.model.entity.sys.ImportFailureLog;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompany;
import com.crm.model.vo.customerManager.CustomerProductDetailMemberCardRawDataVO;
import com.crm.model.vo.customerManager.CustomerVO;
import com.crm.model.vo.orderManager.CustomerProductOrderVO;
import com.crm.model.vo.sys.ImportFailureLogVO;
import com.crm.model.vo.systemManager.MemberShipCardImportVO;
import com.crm.service.common.DictionaryDbService;
import com.crm.service.contractManager.CustomerContractDbService;
import com.crm.service.customerManager.CustomerDbService;
import com.crm.service.customerManager.CustomerProductDetailMemberCardDbService;
import com.crm.service.orderManager.CustomerProductOrderDbService;
import com.crm.service.productManager.CustomerMemberProductDbService;
import com.crm.service.returnMoney.FinishOrderDBservice;
import com.crm.service.sys.ImportFailureLogDbService;
import com.crm.service.sys.UserAccountDbService;
import com.crm.service.sys.permissionManagement.SysCompanyDbService;
import com.google.gson.GsonBuilder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * @Author:baijian
 * @Date: 2021/6/26 9:19
 */
@Slf4j
public class InsertCardCallable implements Callable<JSONObject> {


    private SysCompanyDbService sysCompanyDbService;
    private UserAccountDbService userAccountDbService;
    private CustomerDbService customerDbService;
    private CustomerContractDbService customerContractDbService;
    private CustomerProductOrderDbService customerProductOrderDbService;
    private FinishOrderDBservice finishOrderDBservice;
    private CustomerProductDetailMemberCardDbService customerProductDetailMemberCardDbService;
    private List<CustomerProductDetailMemberCardRawDataVO> list;
    private Map<String,Object> map;
    private CountDownLatch begin;
    private CountDownLatch end;

    public InsertCardCallable(SysCompanyDbService sysCompanyDbService,
                              UserAccountDbService userAccountDbService,
                              CustomerDbService customerDbService,
                              CustomerContractDbService customerContractDbService,
                              CustomerProductOrderDbService customerProductOrderDbService,
                              FinishOrderDBservice finishOrderDBservice,
                              CustomerProductDetailMemberCardDbService customerProductDetailMemberCardDbService,
                              List<CustomerProductDetailMemberCardRawDataVO> list,
                              Map<String,Object> map,
                              CountDownLatch begin, CountDownLatch end) {
        this.sysCompanyDbService = sysCompanyDbService;
        this.userAccountDbService = userAccountDbService;
        this.customerDbService = customerDbService;
        this.customerContractDbService = customerContractDbService;
        this.customerProductOrderDbService = customerProductOrderDbService;
        this.finishOrderDBservice = finishOrderDBservice;
        this.customerProductDetailMemberCardDbService = customerProductDetailMemberCardDbService;
        this.list = list;
        this.map = map;
        this.begin = begin;
        this.end = end;

    }

    @SneakyThrows
    @Override
    public JSONObject call() {

        JSONObject jsonObject = new JSONObject();

        //合同编号为空
        List<Integer> contractList = new ArrayList();
        //成功的条数
        List<Integer>  successList= new ArrayList();

        int a = 0;
        //int b = 0;
        Date date = new Date();
        for(CustomerProductDetailMemberCardRawDataVO raw : list) {
            if(!map.containsKey(raw.getCardNumber())) {
                CustomerProductDetailMemberCard customerProductDetailMemberCard = customerProductDetailMemberCardDbService.getCustomerProductDetailMemberCard(raw.getCardNumber());
                if(customerProductDetailMemberCard == null) {

                    //CustomerProductDetailMemberCard  card = (CustomerProductDetailMemberCard) map.get(raw.getCardNumber());
                    CustomerProductDetailMemberCard  card = new CustomerProductDetailMemberCard();
                    card.setCustomerProductDetailMemberCardId(CommonUtils.getGUID());
                    //log.info("更新前的内容:"+new GsonBuilder().serializeNulls().create().toJson(card));
                    //log.info("更新的内容全量会员卡raw:"+new GsonBuilder().serializeNulls().create().toJson(raw));

                    //电子卡密码
                    if(!StringUtils.isBlank(raw.getElectronicCardPassword())) {
                        card.setMemberCardPassword(raw.getElectronicCardPassword());
                    }

                    //客户id
                    if(!StringUtils.isBlank(raw.getCustomerName())) {
                        CustomerVO customerVO = customerDbService.selectCustomerByName(raw.getCustomerName());
                        if(customerVO != null) {
                            card.setCustomerId(customerVO.getCustomerId());
                        }
                    }

                    //开始日期
                    if(!StringUtils.isBlank(raw.getStartDate())) {
                        card.setStartTm(formatTimeSf(raw.getStartDate()));
                    }

                    //到期时间
                    log.info("到期时间---------"+raw.getExpirationTime());
                    if(!StringUtils.isBlank(raw.getExpirationTime())) {
                        card.setEndTm(formatTimeSf(raw.getExpirationTime()));
                    }

                    //产品单价
                    if(!StringUtils.isBlank(raw.getProductUnitPrice())) {
                        card.setProductPrice(new BigDecimal(raw.getProductUnitPrice()));
                    }

                    //手机号
                    if(!StringUtils.isBlank(raw.getPhoneNumber())){
                        card.setPhone(raw.getPhoneNumber());
                    }


                    if(!StringUtils.isBlank(raw.getCompany())) {
                        SysCompany sysCompany = sysCompanyDbService.selectSysCompanyByName(raw.getCompany());
                        //公司id
                        if(sysCompany != null) {
                            card.setCompanyId(sysCompany.getCompanyId());

                            if(!StringUtils.isBlank(raw.getSalesman())) {
                                UserAccount user = userAccountDbService.selectUserAccountByUserNameAndCompanyId(raw.getSalesman(),sysCompany.getCompanyId());
                                if(user != null) {
                                    //用户id
                                    card.setCreatePeopleId(user.getAccountId());
                                    card.setAccountId(user.getAccountId());
                                }

                            }
                        }
                    }




                    //合同id  合同编号
                    if(!StringUtils.isBlank(raw.getContractNumber())) {
                        CustomerContract customerContract = customerContractDbService.selectContractByContractNumber(raw.getContractNumber());
                        if(customerContract != null) {
                            card.setContractId(customerContract.getContractId());
                            card.setContractNumber(customerContract.getContractNumber());
                        }
                    }
                    //else {
                    //contractList.add(b+2);
                    //log.info(a+"-------------------------合同编号为空");
                    //}

                    //订单id  订单编号
                    if(!StringUtils.isBlank(raw.getOrderNumber())) {
                        CustomerProductOrderVO orderInfoByNumber = customerProductOrderDbService.getOrderInfoByNumber(raw.getOrderNumber());
                        if(orderInfoByNumber != null) {
                            card.setProductOrderId(orderInfoByNumber.getProductOrderId());
                            card.setOrderNumber(orderInfoByNumber.getOrderNumber());


                            List<FinishOrderProduct> finishList = finishOrderDBservice.selectFinishOrderProductList("1","1", orderInfoByNumber.getProductOrderId());
                            if(finishList != null && finishList.size() > 0) {
                                FinishOrderProduct finishOrderProduct = finishList.get(0);
                                card.setMemberProductCode(finishOrderProduct.getProductCode());
                                card.setFinishOrderProductNum(finishOrderProduct.getFinishOrderProductNum());
                                card.setProductPrice(finishOrderProduct.getProductFinalPrice());
                            }
                        }
                    }
                    //else{
                    //log.info(a+"-------------------------订单编号为空");
                    //}


                    //备注
                    if(!StringUtils.isBlank(raw.getRemarks())) {
                        card.setBz(raw.getRemarks());
                    }
                    card.setCreateTm(date);
                    card.setUpdateTm(date);
                    card.setDataSource(1);

                    if(!StringUtils.isBlank(raw.getCardNumber())) {
                        card.setMemberCardNumber(raw.getCardNumber());
                        log.info("更新前的内容:"+new GsonBuilder().serializeNulls().create().toJson(card));
                        customerProductDetailMemberCardDbService.addCustomerProductDetailMemberCard(card);
                        successList.add(a+1);
                        log.info("更新的内容:"+new GsonBuilder().serializeNulls().create().toJson(card));
                    }

                }
            }
        }

        jsonObject.put("repeatIdStr",contractList);
        jsonObject.put("importFailIdStr",successList);

        return jsonObject;
    }


    /**
     * 转时间格式
     * @param date 创建时间  结束时间
     * @return
     */
    public static Date formatTimeSf(String date) throws ParseException {
        //同一个Excel有不一样的时间格式……
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.parse(date);
    }

    public static void main(String[] args) throws ParseException {
        String a = "2022-06-16";
        Date date = formatTimeSf(a);
        System.out.println(date);
    }



    /*public JSONObject call() {

        JSONObject jsonObject = new JSONObject();

        //合同编号为空
        List<Integer> contractList = new ArrayList();
        //成功的条数
        List<Integer>  successList= new ArrayList();

        int a = 0;
        //int b = 0;
        Date date = new Date();
        for(CustomerProductDetailMemberCardRawDataVO raw : list) {
            if(!map.containsKey(raw.getCardNumber())) {
                CustomerProductDetailMemberCard customerProductDetailMemberCard = customerProductDetailMemberCardDbService.getCustomerProductDetailMemberCard(raw.getCardNumber());
                if(customerProductDetailMemberCard == null) {

                    //CustomerProductDetailMemberCard  card = (CustomerProductDetailMemberCard) map.get(raw.getCardNumber());
                    CustomerProductDetailMemberCard  card = new CustomerProductDetailMemberCard();
                    card.setCustomerProductDetailMemberCardId(CommonUtils.getGUID());
                    //log.info("更新前的内容:"+new GsonBuilder().serializeNulls().create().toJson(card));
                    //log.info("更新的内容全量会员卡raw:"+new GsonBuilder().serializeNulls().create().toJson(raw));

                    //电子卡密码
                    if(!StringUtils.isBlank(raw.getElectronicCardPassword())) {
                        card.setMemberCardPassword(raw.getElectronicCardPassword());
                    }

                    //客户id
                    if(!StringUtils.isBlank(raw.getCustomerName())) {
                        CustomerVO customerVO = customerDbService.selectCustomerByName(raw.getCustomerName());
                        if(customerVO != null) {
                            card.setCustomerId(customerVO.getCustomerId());
                        }
                    }

                    //开始日期
                    if(!StringUtils.isBlank(raw.getStartDate())) {
                        card.setStartTm(formatTimeSf(raw.getStartDate()));
                    }

                    //到期时间
                    log.info("到期时间---------"+raw.getExpirationTime());
                    if(!StringUtils.isBlank(raw.getExpirationTime())) {
                        card.setEndTm(formatTimeSf(raw.getExpirationTime()));
                    }

                    //产品单价
                    if(!StringUtils.isBlank(raw.getProductUnitPrice())) {
                        card.setProductPrice(new BigDecimal(raw.getProductUnitPrice()));
                    }

                    //手机号
                    if(!StringUtils.isBlank(raw.getPhoneNumber())){
                        card.setPhone(raw.getPhoneNumber());
                    }


                    if(!StringUtils.isBlank(raw.getCompany())) {
                        SysCompany sysCompany = sysCompanyDbService.selectSysCompanyByName(raw.getCompany());
                        //公司id
                        if(sysCompany != null) {
                            card.setCompanyId(sysCompany.getCompanyId());

                            if(!StringUtils.isBlank(raw.getSalesman())) {
                                UserAccount user = userAccountDbService.selectUserAccountByUserNameAndCompanyId(raw.getSalesman(),sysCompany.getCompanyId());
                                if(user != null) {
                                    //用户id
                                    card.setCreatePeopleId(user.getAccountId());
                                    card.setAccountId(user.getAccountId());
                                }

                            }
                        }
                    }




                    //合同id  合同编号
                    if(!StringUtils.isBlank(raw.getContractNumber())) {
                        CustomerContract customerContract = customerContractDbService.selectContractByContractNumber(raw.getContractNumber());
                        if(customerContract != null) {
                            card.setContractId(customerContract.getContractId());
                            card.setContractNumber(customerContract.getContractNumber());
                        }
                    }
                    //else {
                    //contractList.add(b+2);
                    //log.info(a+"-------------------------合同编号为空");
                    //}

                    //订单id  订单编号
                    if(!StringUtils.isBlank(raw.getOrderNumber())) {
                        CustomerProductOrderVO orderInfoByNumber = customerProductOrderDbService.getOrderInfoByNumber(raw.getOrderNumber());
                        if(orderInfoByNumber != null) {
                            card.setProductOrderId(orderInfoByNumber.getProductOrderId());
                            card.setOrderNumber(orderInfoByNumber.getOrderNumber());


                            List<FinishOrderProduct> finishList = finishOrderDBservice.selectFinishOrderProductList("1","1", orderInfoByNumber.getProductOrderId());
                            if(finishList != null && finishList.size() > 0) {
                                FinishOrderProduct finishOrderProduct = finishList.get(0);
                                card.setMemberProductCode(finishOrderProduct.getProductCode());
                                card.setFinishOrderProductNum(finishOrderProduct.getFinishOrderProductNum());
                                card.setProductPrice(finishOrderProduct.getProductFinalPrice());
                            }
                        }
                    }
                    //else{
                    //log.info(a+"-------------------------订单编号为空");
                    //}


                    //备注
                    if(!StringUtils.isBlank(raw.getRemarks())) {
                        card.setBz(raw.getRemarks());
                    }
                    card.setCreateTm(date);
                    card.setUpdateTm(date);
                    card.setDataSource(1);

                    if(!StringUtils.isBlank(raw.getCardNumber())) {
                        card.setMemberCardNumber(raw.getCardNumber());
                        log.info("更新前的内容:"+new GsonBuilder().serializeNulls().create().toJson(card));
                        customerProductDetailMemberCardDbService.addCustomerProductDetailMemberCard(card);
                        successList.add(a+1);
                        log.info("更新的内容:"+new GsonBuilder().serializeNulls().create().toJson(card));
                    }

                }
            }
        }

        jsonObject.put("repeatIdStr",contractList);
        jsonObject.put("importFailIdStr",successList);

        return jsonObject;
    }*/
}
